package com.zhongyu.user.provider.apiimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhongyu.comm.api.UserApi;
import com.zhongyu.comm.pojo.User;
import com.zhongyu.comm.vo.TokenVo;
import com.zhongyu.user.provider.mapper.UserMapper;
import com.zhongyu.user.provider.toolKit.JwtTool;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class UserApiImpl implements UserApi {
//IOC/DI 将对象的控制权有代码本身转移到外部容器，解耦，面向接口编程
    @Resource
    private UserMapper userMapper;
    @Resource
    private JwtTool jwtTool;

    @Override
    public User findOne(String phone, String password) {
        QueryWrapper<User> queryWrapper= Wrappers.query();
        queryWrapper.eq("phone",phone)
                .eq("password",password);
        User user=userMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public TokenVo getToken(User user) {
        return jwtTool.createJwt(user);
    }

    @Override
    public User getUser(String token) {
        return jwtTool.parseJwt(token);
    }
}
